package zhou.chs.p2_10;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
//implements RadioGroup.OnCheckedChangeListener 声明监听单选选择事件
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener{
    LinearLayout ll1,ll2,ll3;  //声明3个线性布局，分别容纳3个界面的控件
    RadioButton bt1,bt2,bt3;    //声明3个单选按钮，用于选择要显示的界面
    RadioGroup mRg;             //声明1个单选组合框，容纳选择按钮
    TextView tv;
    EditText et;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ll1=(LinearLayout)findViewById(R.id.idll1);     //控件实例化
        ll2=(LinearLayout)findViewById(R.id.idll2);
        ll3=(LinearLayout)findViewById(R.id.idll3);
        bt1=(RadioButton) findViewById(R.id.idbt1);
        bt2=(RadioButton) findViewById(R.id.idbt2);
        bt3=(RadioButton) findViewById(R.id.idbt3);
        tv=(TextView) findViewById(R.id.idtv);
        et=(EditText) findViewById(R.id.idet);
        mRg=(RadioGroup)findViewById(R.id.idrg);
        mRg.setOnCheckedChangeListener(this);     //注册RadioGroup选择事件
        ll1.setVisibility(View.VISIBLE);         //显示界面1
        ll2.setVisibility(View.INVISIBLE);      //隐藏界面2
        ll3.setVisibility(View.INVISIBLE);      //隐藏界面3
        bt1.setBackgroundColor(0xFFFFFFFF);       //按钮1背景颜色白色
        bt2.setBackgroundColor(0xFF00FF00);         //按钮2背景颜色绿色
        bt3.setBackgroundColor(0xFFFFFF00);         //按钮3背景颜色黄色
    }
    @Override
    //按钮选择事件响应
    //选中某一按钮时，显示对应界面，隐藏其他界面，同时选中按钮背景色变为白色
    //按钮未选中时，按钮1背景颜色红色，按钮2背景颜色绿色，按钮1背景颜色黄色，
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        // TODO Auto-generated method stub
        if(checkedId==bt1.getId()){
            ll1.setVisibility(View.VISIBLE);
            ll2.setVisibility(View.INVISIBLE);
            ll3.setVisibility(View.INVISIBLE);
            bt1.setBackgroundColor(0xFFFFFFFF);
            bt2.setBackgroundColor(0xFF00FF00);
            bt3.setBackgroundColor(0xFFFFFF00);
        }else if(checkedId==bt2.getId()){
            ll2.setVisibility(View.VISIBLE);
            ll1.setVisibility(View.INVISIBLE);
            ll3.setVisibility(View.INVISIBLE);
            bt1.setBackgroundColor(0xFFFF0000);
            bt2.setBackgroundColor(0xFFFFFFFF);
            bt3.setBackgroundColor(0xFFFFFF00);
        }else if(checkedId==bt3.getId()){
            ll3.setVisibility(View.VISIBLE);
            ll1.setVisibility(View.INVISIBLE);
            ll2.setVisibility(View.INVISIBLE);
            bt1.setBackgroundColor(0xFFFF0000);
            bt2.setBackgroundColor(0xFF00FF00);
            bt3.setBackgroundColor(0xFFFFFFFF);
        }
        //界面1显示界面3输入的数据
        tv.setText("输入数据=" + et.getText().toString());
    }
}
